<?php
/**
 * User: 五哥派
 * Date: 2024/7/1
 * Tel(同WX): 17335592719
 */
namespace app\api\controller\v1;

use app\api\controller\ApiBase;
use app\api\model\store\Category as CategoryModel;

class Category extends ApiBase {

    protected $model;

    public function __construct(CategoryModel $model){

        parent::__construct();
        $this->model = $model;

    }

    public function getList(){
        $list = $this->model->queryData(['status' => 1, 'pid' => 0], 'id,pid,name,status');
        //$list = $this->model->queryData(['status' => 1], 'id,pid,name,status');
        if (empty($list)) {
            return error('查询失败');
        }

        $tree = [];
        foreach ($list as $k => $item){
            $tree[$k] = $item;
            $list2 = $this->model->queryData(['status' => 1, 'pid' => $item['id']], 'id,pid,name,status,image');
            $tree[$k]['child'] = $list2;
        }

        //$tree = $this->getTree($list);
        //$tree = $this->_children($list, 0);
        //$tree = $this->getTree($list);
        //$result = $this->_reSort($list);
        //$tree = $this->sliceTreeArr($result);
        //dump($tree);die;
        return success('查询成功', ['list' => $tree]);
        //return success('查询成功', ['list' => $list]);
    }

    private function _children($data, $parent_id=0, $isClear=TRUE)
    {
        static $ret = array();
        if($isClear)
            $ret = array();
        foreach ($data as $k => $v)
        {
            if($v['pid'] == $parent_id)
            {
                $ret[]['id'] = $v['id'];
                $ret[]['child'] = $this->_children($data, $v['id'], FALSE);
            }
        }
        return $ret;
    }

    private function _reSort($data, $parent_id=0, $level=0, $isClear=TRUE){

        static $ret = array();
        if($isClear)
            $ret = array();

        foreach ($data as $k => $v) {

            if($v['pid'] == $parent_id) {
                $v['level'] = $level;
                $ret[] = $v;
                $this->_reSort($data, $v['id'], $level+1, FALSE);
            }

        }

        return $ret;
    }

    /**
     * 分类树， 支持无限极分类
     */
    public function getTree($data)
    {
        $items = [];
        foreach ($data as $v) {
            $items[$v['id']] = $v;
        }
        $tree = [];
        foreach ($items as $id => $item) {

            if (isset($items[$item['pid']])) {

                $items[$item['pid']][] = &$items[$id];

            } else {

                $tree[] = &$items[$id];

            }

        }
        return $tree;

    }


    public function sliceTreeArr($data, $firstCount = 5, $secondCount = 3, $threeCount = 5) {
        $data = array_slice($data, 0, $firstCount);
        foreach($data as $k => $v) {
            if(!empty($v['list'])) {
                $data[$k]['list'] = array_slice($v['list'], 0, $secondCount);
                foreach($v['list'] as $kk => $vv) {
                    if(!empty($vv['list'])) {
                        $data[$k]['list'][$kk]['list'] = array_slice($vv['list'], 0, $threeCount);
                    }
                }
            }
        }

        return $data;
    }

}